<?php

define('CACHE_URL', get_bloginfo('template_url') . '/cache/');
define('CACHE_PATH', ABSPATH . 'wp-content/themes/yunjie/cache/');

load_theme_textdomain( 'yunjietheme' );

automatic_feed_links();

if ( function_exists('register_sidebar') ) {
	register_sidebar(array(
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h2 class="widgettitle">',
		'after_title' => '</h2>',
	));
}

/** Comments */
if (function_exists('wp_list_comments')) {
	// comment count
	//add_filter('get_comments_number', 'comment_count', 0);
	function comment_count( $commentcount ) {
		global $id;
		$_commnets = get_comments('post_id=' . $id);
		$comments_by_type = &separate_comments($_commnets);
		return count($comments_by_type['comment']);
	}
}

// custom comments
function yunjie_list_comments($comment, $args, $depth){
	$GLOBALS['comment'] = $comment;
	global $commentcount;
	if(!$commentcount) {
		if (get_option('page_comments')) {
			// get current comment page ID
			$r = wp_parse_args( $args, $defaults );
			$page = $r['page'];
			if (!$page ) {
				$page = 1;
			}
			$commentcount = get_option('comments_per_page') * ($page - 1) + 1;
		} else {
			$commentcount = 1;
		}
	}
?>
	<li <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
		<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
			<div class="comment-info-left">
				<?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, 45 ); ?><br />
				<?php comment_author_link(); ?><br />
				<a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>" <?php if ($depth!=1) echo ' style="display:none"';?> >
				<?php if ($depth==1) {echo $commentcount; $commentcount++;} else echo '#'; ?>楼</a>
				
			</div>
			<div class="comment-info-right">
				<span class="commentdate"><?php echo get_comment_date() . ' ' .  get_comment_time(); ?></span>
				<span class="reply">
					<?php comment_reply_link(array_merge( $args, array('reply_text'=>'(回复此评论)', 'add_below' => 'div-comment' . $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
				</span>
				<span><?php edit_comment_link(__('(Edit)'),'&nbsp;&nbsp;','') ?></span>
				<?php if ($comment->comment_approved == '0') : ?>
					<em><?php _e('您的评论正在等待审核。') ?></em>
					<br />
				<?php endif; ?>
				<div class="comment-text"><?php comment_text() ?></div>
			</div>
			<?php if ($depth==1) { ?><div class="comment-info-cup"><?php if (function_exists('get_cup')) get_cup($comment->comment_author_email); ?></div><?php } ?>

			<div class="fixed"></div>
		</div>
<?php
}

function yunjie_cache_widget($wname) {
	global $wpdb,$post;
	$str = '';
	$option_name = '';
	$save_path = '';
	if ($wname == 'rc') {
		$option_name = 'recent_comments_cache';
		$save_path = CACHE_PATH . 'recentcomments.js';
		$author_filter = " AND user_id='0' AND comment_author_email<>'flarefox@163.com'";
		$maxcount = 10;
		$comments = $wpdb->get_results("SELECT comment_author, comment_author_email, comment_author_url, 
				comment_ID, comment_post_ID, REPLACE(SUBSTRING(comment_content,1,65),'\r\n','')	AS comment_excerpt 
				FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts 
				ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' 
				AND comment_type = '' AND post_password = ''".$author_filter." 
				ORDER BY comment_date_gmt DESC LIMIT " . $maxcount);
	
		$str = "<ul id='ffox_recent_comments'>";
		if ( $comments ) : foreach ($comments as $comment) :
			$str .= "<li class='recentcomments'><a href='". get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . "' title='" . $comment->comment_author . ' on ' . get_the_title($comment->comment_post_ID) . "'>" . $comment->comment_author . '</a> : ' . strip_tags(force_balance_tags($comment->comment_excerpt)) . '</li>';
		endforeach; endif;
		$str .="</ul>";
	} else if ($wname == 'lp') {
		$option_name = 'latest_posts_cache';
		$save_path = CACHE_PATH . 'latestposts.js';
		$posts = get_posts('numberposts=10&orderby=date');
		$str = '<ul>';
		foreach($posts as $post) {
			$str .= "<li><a href='" . get_permalink() . "' title='".$post_tips.get_comments_number() . "'>" . get_the_title() . '</a></li>';
		}
		$str .= '</ul>';
	} else if ($wname == 'rp') {
		$option_name = 'random_posts_cache';
		$save_path = CACHE_PATH . 'randomposts.js';
		$posts = get_posts('numberposts=10&orderby=rand');
		$str = '<ul>';
		foreach($posts as $post) {
			$str .= "<li><a href='" . get_permalink() . "' title='".$post_tips.get_comments_number() . "'>" . get_the_title() . '</a></li>';
		}
		$str .= '</ul>';
	} else {
		return false;
	}
	
	$yunjie_options = get_option('yunjie_theme_options');
	$yunjie_options[$option_name] = $str;
	update_option('yunjie_theme_options', $yunjie_options);

	// cache in file
//	$str = str_replace('"', '\"', $str);
//	$str = str_replace("'", "\'", $str);
//	$str = str_replace('/', '\/', $str);
	$str = str_replace("\r\n",' ', $str);
	$str = str_replace("\n",' ', $str);
	$str = str_replace("\r",' ', $str);
	$str = addslashes($str);
	$str = 'document.write("' . $str . '");';
	$fp = fopen($save_path, 'w');
	fwrite($fp, $str);
	fclose($fp);
}

function yunjie_cache_recent_comments() {
	yunjie_cache_widget('rc');
}

add_action('comment_post', 'yunjie_cache_recent_comments');

function yunjie_get_recent_comments() {
	$yunjie_options = get_option('yunjie_theme_options');
	return $yunjie_options['recent_comments_cache'];
}

function yunjie_get_recent_comments_js() {
	if (!file_exists(CACHE_PATH . 'recentcoments.js'))
		yunjie_cache_recent_comments();
	return CACHE_URL . 'recentcomments.js';
}

function yunjie_cache_latest_posts(){
		yunjie_cache_widget('lp');
}

add_action('edit_post', 'yunjie_cache_latest_posts');
add_action('deleted_post', 'yunjie_cache_latest_posts');

function yunjie_get_latest_posts() {
	$yunjie_options = get_option('yunjie_theme_options');
	return $yunjie_options['latest_posts_cache'];
}

function yunjie_get_latest_posts_js() {
	if (!file_exists(CACHE_PATH . 'latestposts.js'))
		yunjie_cache_latest_posts();
	return CACHE_URL . 'latestposts.js';
}

function yunjie_cache_random_posts(){
	yunjie_cache_widget('rp');
}

add_action('edit_post', 'yunjie_cache_random_posts');
add_action('deleted_post', 'yunjie_cache_random_posts');

function yunjie_get_random_posts() {
	$yunjie_options = get_option('yunjie_theme_options');
	return $yunjie_options['random_posts_cache'];
}

function yunjie_get_random_posts_js() {
	if (!file_exists(CACHE_PATH . 'randomposts.js'))
		yunjie_cache_random_posts();
	return CACHE_URL . 'randomposts.js';
}

/*
* mail to commenter 
*/
/*
// 之所以不用wp自带的get_commentdata()方法，是因为总取不出父评论的内容。
function yunjie_get_comment_data($id) {
	global $wpdb;
	$result = array();

	$query = "select * from $wpdb->comments where comment_ID=" . $id;

	$comments = $wpdb->get_results($query);
	
	if (sizeof($comments) <= 0)
		return $result;
	
	$c = $comments[0];
	$result['comment_ID'] = $c->comment_ID;
	$result['comment_post_ID'] = $c->comment_post_ID;
	$result['comment_author'] = $c->comment_author;
	$result['comment_author_email'] = $c->comment_author_email;
	$result['comment_author_url'] = $c->comment_author_url;
	$result['comment_author_IP'] = $c->comment_author_IP;
	$result['comment_date'] = $c->comment_date;
	$result['comment_approved'] = $c->comment_approved;
	$result['comment_content'] = $c->comment_content;
	$result['comment_approved'] = $c->comment_approved;
	$result['comment_agent'] = $c->comment_agent;
	$result['comment_parent'] = $c->comment_parent;
	$result['user_id'] = $c->user_id;

	return $result;
}

function yunjie_mail_to_commenter($comment_id) {
	global $wpdb;
	
	$child = get_commentdata($comment_id, 1, true);

//	echo $comment_id;
	
//	if ($child['comment_parent'] == 0)
//		return false;
	
	// 这个是从官方找到的get_commentdata的用法:
	// get_commentdata( $comment_ID, $no_cache = 0, $include_unapproved = false )
	$commentdata = get_commentdata($child['comment_parent']);
	$comment_author=$commentdata['comment_author'];
	$commentcontent=$commentdata['comment_content'];
	$parent_id = $commentdata['comment_ID'];
	$post_id=$commentdata['comment_post_ID'];
	$post=get_post($post_id);
	$post_title=$post->post_title;
	$message=$comment_id . ' ' . $parent_id . $comment_author.' 在日志: <em>'.$post_title.'  </em><strong>说 :</strong>  '.$commentcontent;
	 
	$blogname = get_option('blogname');
	$charset = get_option('blog_charset');
	$mail_headers  = "From: $blogname \n" ;
	$mail_headers .= "MIME-Version: 1.0\n";
	$mail_headers .= "Content-Type: text/html;charset=\"$charset\"\n";
	 
	$to='iyuan@localhost';
	$subject='博客评论提醒 来自:'.$comment_author;
	@wp_mail($to, $subject,$message,$mail_headers);
}

add_action('comment_post', 'yunjie_mail_to_commenter');*/

?>
